using GOPATH mode
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#375EAB">

  <title>cobra - The Go Programming Language</title>

<link type="text/css" rel="stylesheet" href="/lib/godoc/style.css">

<link rel="search" type="application/opensearchdescription+xml" title="godoc" href="/opensearch.xml" />


<script>window.initFuncs = [];</script>
<script src="/lib/godoc/jquery.js" defer></script>



<script>var goVersion = "go1.12.12";</script>
<script src="/lib/godoc/godocs.js" defer></script>
</head>
<body>

<div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
...
</div><!-- #lowframe -->

<div id="topbar" class="wide"><div class="container">
<div class="top-heading" id="heading-wide"><a href="/">The Go Programming Language</a></div>
<div class="top-heading" id="heading-narrow"><a href="/">Go</a></div>
<a href="#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
<form method="GET" action="/search">
<div id="menu">
<a href="/doc/">Documents</a>
<a href="/pkg/">Packages</a>
<a href="/project/">The Project</a>
<a href="/help/">Help</a>

<a href="/blog/">Blog</a>


<span class="search-box"><input type="search" id="search" name="q" placeholder="Search" aria-label="Search" required><button type="submit"><span><!-- magnifying glass: --><svg width="24" height="24" viewBox="0 0 24 24"><title>submit search</title><path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"/><path d="M0 0h24v24H0z" fill="none"/></svg></span></button></span>
</div>
</form>

</div></div>



<div id="page" class="wide">
<div class="container">


  <h1>
    Package cobra
    <span class="text-muted"></span>
  </h1>







<div id="nav"></div>


<!--
	Copyright 2009 The Go Authors. All rights reserved.
	Use of this source code is governed by a BSD-style
	license that can be found in the LICENSE file.
-->
<!--
	Note: Static (i.e., not template-generated) href and id
	attributes start with "pkg-" to make it impossible for
	them to conflict with generated attributes (some of which
	correspond to Go identifiers).
-->

	<script>
	document.ANALYSIS_DATA = null;
	document.CALLGRAPH = null;
	</script>

	
		
		<div id="short-nav">
			<dl>
			<dd><code>import "cobra"</code></dd>
			</dl>
			<dl>
			<dd><a href="#pkg-overview" class="overviewLink">Overview</a></dd>
			<dd><a href="#pkg-index" class="indexLink">Index</a></dd>
			
			
			</dl>
		</div>
		<!-- The package's Name is printed as title by the top-level template -->
		<div id="pkg-overview" class="toggleVisible">
			<div class="collapsed">
				<h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
			</div>
			<div class="expanded">
				<h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
				
				
			</div>
		</div>

		<div id="pkg-index" class="toggleVisible">
		<div class="collapsed">
			<h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
		</div>
		<div class="expanded">
			<h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>

		<!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
			<div id="manual-nav">
			<dl>
			
			
			
			
				
				<dd><a href="#Command">type Command</a></dd>
				
				
					
					<dd>&nbsp; &nbsp; <a href="#Command.AddCommand">func (c *Command) AddCommand(cmds ...*Command)</a></dd>
				
					
					<dd>&nbsp; &nbsp; <a href="#Command.Execute">func (c *Command) Execute() error</a></dd>
				
					
					<dd>&nbsp; &nbsp; <a href="#Command.ExecuteC">func (c *Command) ExecuteC() (cmd *Command, err error)</a></dd>
				
					
					<dd>&nbsp; &nbsp; <a href="#Command.Find">func (c *Command) Find(args []string) (*Command, []string, error)</a></dd>
				
					
					<dd>&nbsp; &nbsp; <a href="#Command.Flags">func (c *Command) Flags() *flag.FlagSet</a></dd>
				
					
					<dd>&nbsp; &nbsp; <a href="#Command.HasParent">func (c *Command) HasParent() bool</a></dd>
				
					
					<dd>&nbsp; &nbsp; <a href="#Command.HasSubCommands">func (c *Command) HasSubCommands() bool</a></dd>
				
					
					<dd>&nbsp; &nbsp; <a href="#Command.Help">func (c *Command) Help() error</a></dd>
				
					
					<dd>&nbsp; &nbsp; <a href="#Command.HelpFunc">func (c *Command) HelpFunc() func(*Command, []string)</a></dd>
				
					
					<dd>&nbsp; &nbsp; <a href="#Command.InitDefaultHelpCmd">func (c *Command) InitDefaultHelpCmd()</a></dd>
				
					
					<dd>&nbsp; &nbsp; <a href="#Command.InitDefaultHelpFlag">func (c *Command) InitDefaultHelpFlag()</a></dd>
				
					
					<dd>&nbsp; &nbsp; <a href="#Command.Name">func (c *Command) Name() string</a></dd>
				
					
					<dd>&nbsp; &nbsp; <a href="#Command.Parent">func (c *Command) Parent() *Command</a></dd>
				
					
					<dd>&nbsp; &nbsp; <a href="#Command.ParseFlags">func (c *Command) ParseFlags(args []string) error</a></dd>
				
					
					<dd>&nbsp; &nbsp; <a href="#Command.RemoveCommand">func (c *Command) RemoveCommand(cmds ...*Command)</a></dd>
				
					
					<dd>&nbsp; &nbsp; <a href="#Command.Root">func (c *Command) Root() *Command</a></dd>
				
					
					<dd>&nbsp; &nbsp; <a href="#Command.Runnable">func (c *Command) Runnable() bool</a></dd>
				
					
					<dd>&nbsp; &nbsp; <a href="#Command.SetHelpCommand">func (c *Command) SetHelpCommand(cmd *Command)</a></dd>
				
					
					<dd>&nbsp; &nbsp; <a href="#Command.SetHelpFunc">func (c *Command) SetHelpFunc(f func(*Command, []string))</a></dd>
				
					
					<dd>&nbsp; &nbsp; <a href="#Command.Traverse">func (c *Command) Traverse(args []string) (*Command, []string, error)</a></dd>
				
			
			
			</dl>
			</div><!-- #manual-nav -->

		

		
			<h3>Package files</h3>
			<p>
			<span style="font-size:90%">
			
				<a href="/src/cobra/Command.go">Command.go</a>
			
				<a href="/src/cobra/Help.go">Help.go</a>
			
			</span>
			</p>
		
		</div><!-- .expanded -->
		</div><!-- #pkg-index -->

		

		
		
		
		
			
			
			<h2 id="Command">type <a href="/src/cobra/Command.go?s=272:1217#L4">Command</a>
				<a class="permalink" href="#Command">&#xb6;</a>
				
				
			</h2>
			<p>
Command结构体定义了该cobra程序所需要的一些指令
此处是简化版，提供了Use, Short, Long和Run四大基本字段以及一些其他的辅助字段
</p>

			<pre>type Command struct {
    <span class="comment">//Use is the one-line usage message</span>
<span id="Command.Use"></span>    Use <a href="/pkg/builtin/#string">string</a>
    <span class="comment">//Short is the short decription shown in the &#39;help&#39; output</span>
<span id="Command.Short"></span>    Short <a href="/pkg/builtin/#string">string</a>
    <span class="comment">//Long is the long message shown in the &#39;help&lt;this-command&gt;&#39; output</span>
<span id="Command.Long"></span>    Long <a href="/pkg/builtin/#string">string</a>
    <span class="comment">//Run: Typically the actual work function. Most commands will only implement this.</span>
<span id="Command.Run"></span>    Run func(cmd *<a href="#Command">Command</a>, args []<a href="/pkg/builtin/#string">string</a>)

    <span class="comment">//TraverseChildren parses flags on all parents before executing child command.</span>
<span id="Command.TraverseChildren"></span>    TraverseChildren <a href="/pkg/builtin/#bool">bool</a>
    <span class="comment">// contains filtered or unexported fields</span>
}
</pre>

			

			

			
			
			

			

			
				
				<h3 id="Command.AddCommand">func (*Command) <a href="/src/cobra/Command.go?s=3075:3121#L119">AddCommand</a>
					<a class="permalink" href="#Command.AddCommand">&#xb6;</a>
					
					
				</h3>
				<pre>func (c *<a href="#Command">Command</a>) AddCommand(cmds ...*<a href="#Command">Command</a>)</pre>
				<p>
AddCommand adds one or more commands to this parent command.
</p>

				
				
				
			
				
				<h3 id="Command.Execute">func (*Command) <a href="/src/cobra/Command.go?s=1379:1412#L36">Execute</a>
					<a class="permalink" href="#Command.Execute">&#xb6;</a>
					
					
				</h3>
				<pre>func (c *<a href="#Command">Command</a>) Execute() <a href="/pkg/builtin/#error">error</a></pre>
				<p>
Execute uses the args (os.Args[1:] by default)
and run through the command tree finding appropriate matches
for commands and then corresponding flags.
</p>

				
				
				
			
				
				<h3 id="Command.ExecuteC">func (*Command) <a href="/src/cobra/Command.go?s=1488:1542#L42">ExecuteC</a>
					<a class="permalink" href="#Command.ExecuteC">&#xb6;</a>
					
					
				</h3>
				<pre>func (c *<a href="#Command">Command</a>) ExecuteC() (cmd *<a href="#Command">Command</a>, err <a href="/pkg/builtin/#error">error</a>)</pre>
				<p>
ExecuteC executes the command.
</p>

				
				
				
			
				
				<h3 id="Command.Find">func (*Command) <a href="/src/cobra/Command.go?s=6193:6258#L246">Find</a>
					<a class="permalink" href="#Command.Find">&#xb6;</a>
					
					
				</h3>
				<pre>func (c *<a href="#Command">Command</a>) Find(args []<a href="/pkg/builtin/#string">string</a>) (*<a href="#Command">Command</a>, []<a href="/pkg/builtin/#string">string</a>, <a href="/pkg/builtin/#error">error</a>)</pre>
				<p>
Find the target command given the args and command tree
Meant to be run on the highest node. Only searches down.
</p>

				
				
				
			
				
				<h3 id="Command.Flags">func (*Command) <a href="/src/cobra/Command.go?s=7798:7837#L311">Flags</a>
					<a class="permalink" href="#Command.Flags">&#xb6;</a>
					
					
				</h3>
				<pre>func (c *<a href="#Command">Command</a>) Flags() *<a href="/pkg/github.com/spf13/pflag/">flag</a>.<a href="/pkg/github.com/spf13/pflag/#FlagSet">FlagSet</a></pre>
				<p>
Flags returns the complete FlagSet that applies
to this command (local and persistent declared here and by all parents).
</p>

				
				
				
			
				
				<h3 id="Command.HasParent">func (*Command) <a href="/src/cobra/Command.go?s=8261:8295#L330">HasParent</a>
					<a class="permalink" href="#Command.HasParent">&#xb6;</a>
					
					
				</h3>
				<pre>func (c *<a href="#Command">Command</a>) HasParent() <a href="/pkg/builtin/#bool">bool</a></pre>
				<p>
HasParent determines if the command is a child command.
</p>

				
				
				
			
				
				<h3 id="Command.HasSubCommands">func (*Command) <a href="/src/cobra/Command.go?s=8129:8168#L325">HasSubCommands</a>
					<a class="permalink" href="#Command.HasSubCommands">&#xb6;</a>
					
					
				</h3>
				<pre>func (c *<a href="#Command">Command</a>) HasSubCommands() <a href="/pkg/builtin/#bool">bool</a></pre>
				<p>
HasSubCommands determines if the command has children commands.
</p>

				
				
				
			
				
				<h3 id="Command.Help">func (*Command) <a href="/src/cobra/Help.go?s=164:194#L1">Help</a>
					<a class="permalink" href="#Command.Help">&#xb6;</a>
					
					
				</h3>
				<pre>func (c *<a href="#Command">Command</a>) Help() <a href="/pkg/builtin/#error">error</a></pre>
				<p>
Help puts out the help for the command.
Used when a user calls help [command].
Can be defined by user by overriding HelpFunc.
</p>

				
				
				
			
				
				<h3 id="Command.HelpFunc">func (*Command) <a href="/src/cobra/Help.go?s=386:439#L5">HelpFunc</a>
					<a class="permalink" href="#Command.HelpFunc">&#xb6;</a>
					
					
				</h3>
				<pre>func (c *<a href="#Command">Command</a>) HelpFunc() func(*<a href="#Command">Command</a>, []<a href="/pkg/builtin/#string">string</a>)</pre>
				<p>
HelpFunc returns either the function set by SetHelpFunc for this command
or a parent, or it returns a function with default help behavior.
</p>

				
				
				
			
				
				<h3 id="Command.InitDefaultHelpCmd">func (*Command) <a href="/src/cobra/Help.go?s=1195:1233#L33">InitDefaultHelpCmd</a>
					<a class="permalink" href="#Command.InitDefaultHelpCmd">&#xb6;</a>
					
					
				</h3>
				<pre>func (c *<a href="#Command">Command</a>) InitDefaultHelpCmd()</pre>
				<p>
InitDefaultHelpCmd adds default help command to c.
It is called automatically by executing the c or by calling help and usage.
If c already has help command or c has no subcommands, it will do nothing.
</p>

				
				
				
			
				
				<h3 id="Command.InitDefaultHelpFlag">func (*Command) <a href="/src/cobra/Help.go?s=2062:2101#L62">InitDefaultHelpFlag</a>
					<a class="permalink" href="#Command.InitDefaultHelpFlag">&#xb6;</a>
					
					
				</h3>
				<pre>func (c *<a href="#Command">Command</a>) InitDefaultHelpFlag()</pre>
				<p>
InitDefaultHelpFlag adds default help flag to c.
It is called automatically by executing the c or by calling help and usage.
If c already has help flag, it will do nothing.
</p>

				
				
				
			
				
				<h3 id="Command.Name">func (*Command) <a href="/src/cobra/Command.go?s=8496:8527#L340">Name</a>
					<a class="permalink" href="#Command.Name">&#xb6;</a>
					
					
				</h3>
				<pre>func (c *<a href="#Command">Command</a>) Name() <a href="/pkg/builtin/#string">string</a></pre>
				<p>
Name returns the command&#39;s name: the first word in the use line.
</p>

				
				
				
			
				
				<h3 id="Command.Parent">func (*Command) <a href="/src/cobra/Command.go?s=8370:8405#L335">Parent</a>
					<a class="permalink" href="#Command.Parent">&#xb6;</a>
					
					
				</h3>
				<pre>func (c *<a href="#Command">Command</a>) Parent() *<a href="#Command">Command</a></pre>
				<p>
Parent returns a commands parent command.
</p>

				
				
				
			
				
				<h3 id="Command.ParseFlags">func (*Command) <a href="/src/cobra/Command.go?s=7029:7078#L284">ParseFlags</a>
					<a class="permalink" href="#Command.ParseFlags">&#xb6;</a>
					
					
				</h3>
				<pre>func (c *<a href="#Command">Command</a>) ParseFlags(args []<a href="/pkg/builtin/#string">string</a>) <a href="/pkg/builtin/#error">error</a></pre>
				<p>
ParseFlags parses persistent flag tree and local flags.
</p>

				
				
				
			
				
				<h3 id="Command.RemoveCommand">func (*Command) <a href="/src/cobra/Command.go?s=3354:3403#L130">RemoveCommand</a>
					<a class="permalink" href="#Command.RemoveCommand">&#xb6;</a>
					
					
				</h3>
				<pre>func (c *<a href="#Command">Command</a>) RemoveCommand(cmds ...*<a href="#Command">Command</a>)</pre>
				<p>
RemoveCommand removes one or more commands from a parent command.
</p>

				
				
				
			
				
				<h3 id="Command.Root">func (*Command) <a href="/src/cobra/Command.go?s=8654:8687#L350">Root</a>
					<a class="permalink" href="#Command.Root">&#xb6;</a>
					
					
				</h3>
				<pre>func (c *<a href="#Command">Command</a>) Root() *<a href="#Command">Command</a></pre>
				<p>
Root finds root command.
</p>

				
				
				
			
				
				<h3 id="Command.Runnable">func (*Command) <a href="/src/cobra/Command.go?s=8002:8035#L320">Runnable</a>
					<a class="permalink" href="#Command.Runnable">&#xb6;</a>
					
					
				</h3>
				<pre>func (c *<a href="#Command">Command</a>) Runnable() <a href="/pkg/builtin/#bool">bool</a></pre>
				<p>
Runnable determines if the command is itself runnable.
</p>

				
				
				
			
				
				<h3 id="Command.SetHelpCommand">func (*Command) <a href="/src/cobra/Help.go?s=2483:2529#L80">SetHelpCommand</a>
					<a class="permalink" href="#Command.SetHelpCommand">&#xb6;</a>
					
					
				</h3>
				<pre>func (c *<a href="#Command">Command</a>) SetHelpCommand(cmd *<a href="#Command">Command</a>)</pre>
				<p>
SetHelpCommand sets help command.
</p>

				
				
				
			
				
				<h3 id="Command.SetHelpFunc">func (*Command) <a href="/src/cobra/Help.go?s=2367:2424#L75">SetHelpFunc</a>
					<a class="permalink" href="#Command.SetHelpFunc">&#xb6;</a>
					
					
				</h3>
				<pre>func (c *<a href="#Command">Command</a>) SetHelpFunc(f func(*<a href="#Command">Command</a>, []<a href="/pkg/builtin/#string">string</a>))</pre>
				<p>
SetHelpFunc sets help function. Can be defined by Application.
</p>

				
				
				
			
				
				<h3 id="Command.Traverse">func (*Command) <a href="/src/cobra/Command.go?s=3730:3799#L146">Traverse</a>
					<a class="permalink" href="#Command.Traverse">&#xb6;</a>
					
					
				</h3>
				<pre>func (c *<a href="#Command">Command</a>) Traverse(args []<a href="/pkg/builtin/#string">string</a>) (*<a href="#Command">Command</a>, []<a href="/pkg/builtin/#string">string</a>, <a href="/pkg/builtin/#error">error</a>)</pre>
				<p>
Traverse the command tree to find the command, and parse args for each parent.
</p>

				
				
				
			
		
	

	







<div id="footer">
Build version go1.12.12.<br>
Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
the content of this page is licensed under the
Creative Commons Attribution 3.0 License,
and code is licensed under a <a href="/LICENSE">BSD license</a>.<br>
<a href="/doc/tos.html">Terms of Service</a> |
<a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
</div>

</div><!-- .container -->
</div><!-- #page -->
</body>
</html>

